<?php
/**
 * 投资项目项目
 */
namespace app\model;

use think\Model;
use app\model\Chain;

class InvestmentModel extends Model
{
	protected $name = 'wqq_game_projects';
	/**
	 * 投资项目项目列表
	 *
	 */
	public function GetList($where){
		$InvestmentModel=new InvestmentModel;
		$result=$InvestmentModel->field('id,title,imgs,invest_money,label_id')->where($where)->order('top_time desc,id desc')->paginate()->toArray();
		return $result;
	}
	/**
	 * 投资项目项目详情
	 *
	 */
	public function detail($id){
		$InvestmentModel = InvestmentModel::find($id);
		return $InvestmentModel;
	}
	/**
	 * 添加
	 *
	 */
	public function AddData($param){
		$InvestmentModel = new InvestmentModel;
		$param['create_time']=time();
		$param['update_time']=time();
		$param['uniacid']=config('app.wxapp_uniacid');
		/* if(isset($param['chain_id'])&&!empty($param['chain_id'])){
		 $ChainModel=new Chain();
		 $ids=explode(',', $param['chain_id']);
		 $chain_id=[];
		 foreach ($ids as $val){
		 $chain_id[]=$ChainModel->GetChainIdById($val);
		 }
		 $param['chain_id']=implode(',', $chain_id);
		 } */
		$field=['uniacid','name','desc','address','video','imgs','content','create_time','update_time','role_id','chain_id'];
		$InvestmentModel->allowField($field)->save($param);
		$id=$InvestmentModel->id;
		
		//生成小程序二维码
		
		$data = array(
				'scene' => $id,
				'page' => 'pages/localproject/detail',
				'width' => 280,
		);
		$url= construct_XCX_Code($data,config('app.wxapp_uniacid'));
		$InvestmentModel->update(['code_url'=>$url,'id'=>$id]);
		return $id;
	}
	/**
	 * 编辑
	 *
	 */
	public function EditData($param,$id){
		$InvestmentModel = InvestmentModel::find($id);
		$param['update_time']=time();
		/* if(isset($param['chain_id'])&&!empty($param['chain_id'])){
		 $ChainModel=new Chain();
		 $ids=explode(',', $param['chain_id']);
		 $chain_id=[];
		 foreach ($ids as $val){
		 $chain_id[]=$ChainModel->GetChainIdById($val);
		 }
		 $param['chain_id']=implode(',', $chain_id);
		 } */
		
		$field=['name','desc','address','video','imgs','content','create_time','update_time','role_id','chain_id'];
		
		$InvestmentModel->allowField($field)->save($param);
		
		
		//生成小程序二维码
		$data = array(
				'scene' => $id,
				'page' => 'pages/localproject/detail',
				'width' => 280,
		);
		$url= construct_XCX_Code($data,config('app.wxapp_uniacid'));
		$InvestmentModel->update(['code_url'=>$url,'id'=>$id]);
		return $id;
	}
	/**
	 * 修改禁用状态
	 *
	 */
	public function UpdateStatus($id){
		$InvestmentModel = InvestmentModel::find($id);
		$InvestmentModel->status=$InvestmentModel->status==1?0:1;
		$InvestmentModel->save();
		return true;
	}
	/**
	 * 删除
	 *
	 */
	public function Del($param){
		$update=[];
		foreach($param as $val){
			$update[]=['id'=>$val,'status'=>-1,'deletetime'=>time()];
		}
		if(!empty($update)){
			$InvestmentModel = new InvestmentModel;
			$InvestmentModel->saveAll($update);
		}
		return true;
	}
	/**
	 * 投资项目数量统计
	 *
	 */
	public function GetTotal($where){
		$InvestmentModel=new InvestmentModel;
		$result=$InvestmentModel->where($where)->count();
		return $result;
	}
}